<script setup lang="ts">
import EvaluateCard from './EvaluateCard.vue'
import type { Message, Prescription } from '@/types/room'
import { MsgType, PrescriptionStatus } from '@/enums'
import type { Image } from '@/types/consult'
import { ImagePreview, Toast } from 'vant'
import { useUserStore } from '@/stores'
import dayjs from 'dayjs'
import { useRouter } from 'vue-router'
import { useShowprescription } from '@/composable'
import { getConsultFlagText, getIllnessTimeText } from '@/utils/filter'

// import router from '@/router'  // 不建议使用
// 嘛的  大BUG
// import list from 'vant/lib/list'
// import list from 'vant/lib/list'

defineProps<{
  list: Message[]
}>()

// 格式化时间
const formatTime = (time: string) => {
  return dayjs(time).format('HH:mm')
}

// 查看大图 图片上传是可选的，这里参数配置泛型也是可选的，因此需要所以做 守卫
const previewImgs = (pictures?: Image[]) => {
  pictures && pictures.length && ImagePreview(pictures.map((item) => item.url))
}

// 拿到患者的id,然后根据患者的id和医生的id来分别渲染是谁的话
const store = useUserStore()

// 处方状态不同此按钮操作不同：
// 如果处方失效：提示即可
// 如果没付款且有订单ID，代表已经生成订单没付款：去订单详情付款
// 如果没付款且没订单ID：去预支付页面
// 原生处方

const { showPrescription } = useShowprescription()

const router = useRouter()
// 购买处方
const buy = (prescription?: Prescription) => {
  // console.log('购买处方', prescription)
  if (prescription) {
    // 如果处方失效：提示即可
    if (prescription.status === PrescriptionStatus.Invalid) {
      return Toast('处方已失效')
    }
    // 如果没付款且有订单ID，代表已经生成订单没付款：去订单详情付款
    if (prescription.status === PrescriptionStatus.NotPayment && prescription.orderId) {
      // prescription.orderId 药品id
      return router.push(`/order/${prescription.orderId}`)
    }
    // 如果没付款且没订单ID：去预支付页面
    if (prescription.status === PrescriptionStatus.NotPayment && !prescription.orderId) {
      return router.push(`/order/pay?id=${prescription.id}`)
    }
  }
}
</script>

<template>
  <!-- v-for="{ id, msgType, from, fromAvatar, to, toAvatar, createTime, msg } in list" -->
  <template v-for="{ id, msgType, msg, from, createTime, fromAvatar } in list" :key="id">
    <!-- 病情描述 -->
    <div class="msg msg-illness" v-if="msgType === MsgType.CardPat">
      <div class="patient van-hairline--bottom">
        <p>
          {{ msg.consultRecord?.patientInfo.name }} {{ msg.consultRecord?.patientInfo.name }}
          {{ msg.consultRecord?.patientInfo.age }}岁
        </p>
        <p>
          {{ getIllnessTimeText(msg.consultRecord?.illnessTime!) }} |
          {{ getConsultFlagText(msg.consultRecord?.consultFlag as 0 | 1) }}
        </p>
      </div>
      <van-row>
        <van-col span="6">病情描述</van-col>
        <van-col span="18">{{ msg.consultRecord?.illnessDesc }}</van-col>
        <van-col span="6">图片</van-col>
        <van-col span="18" @click="previewImgs(msg.consultRecord?.pictures)">点击查看</van-col>
      </van-row>
    </div>

    <!-- 温馨提示-通知 -->
    <div class="msg msg-tip" v-if="msgType === MsgType.NotifyTip">
      <div class="content">
        <span class="green">温馨提示：</span>
        <span>{{ msg.content }}</span>
      </div>
    </div>

    <!-- 通知 -->
    <div class="msg msg-tip" v-if="msgType === MsgType.Notify">
      <div class="content">
        <span>{{ msg.content }}</span>
      </div>
    </div>

    <!-- 患者发送文字 -->
    <div class="msg msg-to" v-if="msgType === MsgType.MsgText && from === store.user?.id">
      <div class="content">
        <div class="time">{{ formatTime(createTime) }}</div>
        <div class="pao">{{ msg.content }}</div>
      </div>
      <van-image :src="store.user?.avatar" />
    </div>

    <!-- 患者发送图片 -->
    <div class="msg msg-to" v-if="msgType === MsgType.MsgImage && from === store.user?.id">
      <div class="content">
        <div class="time">{{ formatTime(createTime) }}</div>
        <van-image fit="contain" :src="msg.picture?.url" />
      </div>
      <van-image :src="store.user?.avatar" />
    </div>

    <!-- 医生 发送文字 -->
    <div class="msg msg-from" v-if="msgType === MsgType.MsgText && from !== store.user?.id">
      <van-image :src="fromAvatar" />
      <div class="content">
        <div class="time">{{ formatTime(createTime) }}</div>
        <div class="pao">{{ msg.content }}</div>
      </div>
    </div>

    <!-- 医生 发送图片 -->
    <div class="msg msg-from" v-if="msgType === MsgType.MsgImage && from !== store.user?.id">
      <van-image :src="fromAvatar" />
      <div class="content">
        <van-image :src="msg.picture?.url" />
      </div>
    </div>

    <!-- 处方消息 -->
    <div class="msg msg-recipe" v-if="msgType === MsgType.CardPre">
      <div class="content">
        <div class="head van-hairline--bottom">
          <div class="head-tit">
            <h3>电子处方</h3>
            <p @click="showPrescription(msg.prescription?.id)">
              原始处方 <van-icon name="arrow"></van-icon>
            </p>
          </div>
          <p>
            {{ msg.prescription?.name }} {{ msg.prescription?.genderValue }}
            {{ msg.prescription?.age }}岁 {{ msg.prescription?.diagnosis }}
          </p>
          <p>开方时间：{{ msg.prescription?.createTime }}</p>
        </div>
        <div class="body">
          <div class="body-item" v-for="med in msg.prescription?.medicines" :key="med.id">
            <div class="durg">
              <p>{{ med.name }} {{ med.specs }}</p>
              <p>{{ med.usageDosag }}</p>
            </div>
            <div class="num">x{{ med.quantity }}</div>
          </div>
        </div>
        <div class="foot"><span @click="buy(msg.prescription)">购买药品</span></div>
      </div>
    </div>

    <!-- 评价，没有评价就显示填写评价的卡片，有评价就展示评价 -->
    <div class="msg" v-if="msgType === MsgType.CardEva || msgType === MsgType.CardEvaForm">
      <EvaluateCard :evaluateDoc="msg.evaluateDoc" />
    </div>

    <!-- 取消订单 -->
    <div class="msg msg-tip msg-tip-cancel" v-if="msgType === MsgType.NotifyCancel">
      <div class="content">
        <span>{{ msg.content }}</span>
      </div>
    </div>
  </template>
</template>

<style lang="scss" scoped>
@import '@/styles/room.scss';
</style>
